const utils = require("./utils");
const defaults = require("./default");
const bind = require("./bind");
const dispatchRequest=require('./dispatchRequest')
class Axios {
  constructor() {
    this.defaults = defaults;
  }
  request(config) {
    config=Object.assign(defaults,this.defaults,{method:'get'},config)
    config.method = config.method.toLowerCase();
    var chain = [dispatchRequest, undefined];
    var promise = Promise.resolve(config);
    while (chain.length) {
      let fn = chain.shift();
      promise = promise.then(fn, chain.shift());
    }
    return promise
  }
}
utils.forEach(["get", "delete"], function (method) {
  Axios.prototype[method] = function (url, config) {
    return this.request(
      utils.merge(config || {}, {
        url,
        method,
      })
    );
  };
});
function createAxios(defaults) {
  const context = new Axios(defaults);
  let interface = bind(Axios.prototype.request, context);
  interface = utils.extend(interface, Axios.prototype, context);
  interface = utils.extend(interface, context);
  return interface;
}
const axios = createAxios(defaults);

module.exports = axios;
